package com.metaera.app.ui.adapters;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.FollowAuthorPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.FollowAuthorResponse;
import com.metaera.app.utils.image.GlideApp;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;

public class FollowAuthorAdapter extends BaseQuickAdapter<FollowAuthorResponse, BaseViewHolder> {

    public FollowAuthorAdapter() {
        super(R.layout.item_follow_author);
    }


    @Override
    protected void convert(@NonNull BaseViewHolder holder, FollowAuthorResponse item) {
        SwitchCompat switchCompat = holder.getView(R.id.switch_compat);
        ImageView ivAvatar = holder.getView(R.id.iv_avatar);

        List<FollowAuthorResponse.Users> users = item.getUsers();
        FollowAuthorResponse.Users user = new FollowAuthorResponse.Users();
        if (users != null && !users.isEmpty()){
            user = users.get(0);

            switchCompat.setChecked(item.getIs_notice() == 1);

            FollowAuthorPresenter presenter = new FollowAuthorPresenter();
            presenter.setModelAndView(new BasicNetworkRequestView<>());

            switchCompat.setOnCheckedChangeListener((buttonView, isChecked) -> {
                presenter.notice(item.getId(), isChecked);
            });
        }

        holder.setText(R.id.tv_nickname, user.getNickname());

        GlideApp.with(ivAvatar)
                .load(user.getAvatar_img())
                .avatar()
                .into(ivAvatar);
    }

}
